package com.lyhengtongwl.zqsnews.newsNotify;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;

public class NotificationUtils {
    /**
     * 开柜记录消息提醒
     *
     * @param context 上下文对象
     * @param title   标题
     * @param msg     内容
     */
    public static void showOpenHistoryNotice(Context context, String title, String msg, int id) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 创建
            NotificationChannel channel = new NotificationChannel(String.valueOf(id), "通知", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);
            channel.setLightColor(ContextCompat.getColor(context, R.color.mainBg));
            channel.setShowBadge(true);
            channel.setDescription("赚钱树");
            assert manager != null;
            manager.createNotificationChannel(channel);
        }
        //点击启动页面
        Intent clickIntent = new Intent(context, MainActivity.class);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, String.valueOf(id));
        mBuilder.setContentTitle(title)
                .setContentText(msg)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo))
                .setOnlyAlertOnce(true)
                .setPriority(Notification.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.logo)
                .setColor(ContextCompat.getColor(context, R.color.transparent));
        clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent clickPendingIntent = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        // 点击删除
        Intent cancelIntent = new Intent(context, NoticeCancelBroadcastReceiver.class);
        cancelIntent.setAction("notice_cancel");
        cancelIntent.putExtra("id", id);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_ONE_SHOT);
        mBuilder.setContentIntent(clickPendingIntent);
        mBuilder.setDeleteIntent(cancelPendingIntent);
        mBuilder.setAutoCancel(true);
        assert manager != null;
        manager.notify(id, mBuilder.build());

    }
}
